package com.leyou.user.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.auth.utils.UserContext;
import com.leyou.user.dto.AddressDTO;
import com.leyou.user.dto.UserDetailDTO;
import com.leyou.user.entity.UserAddress;
import com.leyou.user.entity.UserDetail;
import com.leyou.user.service.UserAddressService;
import com.leyou.user.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("address")
public class AddressController {

    private final UserAddressService userAddressService;

    public AddressController(UserAddressService userAddressService) {
        this.userAddressService = userAddressService;
    }

    /**
     * 根据
     *
     * @param id 地址id
     * @return 地址信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<AddressDTO> queryAddressById(@PathVariable("id") Long id) {
        UserAddress userAddress = userAddressService.getById(id);
        AddressDTO address = new AddressDTO(userAddress);
        return ResponseEntity.ok(address);
    }

    @GetMapping("/default/{id}")
    public ResponseEntity<AddressDTO> isDefault(@PathVariable("id") Long id) {
        Long userId = UserContext.getUser().getId();
        boolean update = userAddressService.update().set("is_default", 0).eq("user_id", userId).update();
        boolean one = userAddressService.update().set("is_default", 1).eq("id", id).update();
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("list")
    public ResponseEntity<List<AddressDTO>> queryAddress() {
        Long userId = UserContext.getUser().getId();
        List<UserAddress> list = userAddressService.query().eq("user_id", userId).list();
        List<AddressDTO> addressDTOS = AddressDTO.convertEntityList(list);
        for (AddressDTO addressDTO : addressDTOS) {
            addressDTO.setAddress(addressDTO.getProvince() + " " + addressDTO.getCity() + " " + addressDTO.getDistrict() + " " + addressDTO.getStreet());
            if (addressDTO.getPhone().length() == 11) {
                addressDTO.setPhone(addressDTO.getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
            }
        }
        return ResponseEntity.ok(addressDTOS);
    }

    @PostMapping
    public ResponseEntity<Void> saveAddress(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "addressee", required = false, defaultValue = "") String addressee,
            @RequestParam(value = "province", required = false, defaultValue = "") String province,
            @RequestParam(value = "city", required = false, defaultValue = "") String city,
            @RequestParam(value = "district", required = false, defaultValue = "") String district,
            @RequestParam(value = "street", required = false, defaultValue = "") String street,
            @RequestParam(value = "phone", required = false, defaultValue = "") String phone,
            @RequestParam(value = "postcode", required = false, defaultValue = "") String postcode) {

        Long userId = UserContext.getUser().getId();
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(UserContext.getUser().getId());
        userAddress.setAddressee(addressee);
        userAddress.setProvince(province);
        userAddress.setCity(city);
        userAddress.setDistrict(district);
        userAddress.setStreet(street);
        userAddress.setPhone(phone);
        userAddress.setPostcode(postcode);
        userAddress.setUserId(userId);
        if (id != null && id > 0) {
            userAddress.setId(id);
        }
        userAddressService.saveOrUpdate(userAddress);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteById(@PathVariable("id") Long id) {
        // 删除表
        userAddressService.removeById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    @GetMapping("hello")
    public ResponseEntity<String> hello() {
        return ResponseEntity.ok("上海浦东新区航头镇航头路18号传智播客");
    }
}